$(document).ready(function(){
    var sym_page_add='';
    //Add
    $('.sym_page .symbiosis-button-add').click(function(){
        if($('#sym_page_add').length){
            $('#sym_page_add').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_page_add" title="Page adding"></div>');
            $('#sym_page_add').dialog({
                modal: true,
                width: 310,
                position: ['center', 100],
                open: function(event, ui) {
                    if(sym_page_add){
                        symbiont='page.add.'+sym_page_add;
                        sym_page_add='';
                    }
                    else{
                        symbiont='page.add'
                    }
                    $.ajax({
                        data:{
                            symbiont: symbiont
                        },
                        success:function(r){
                            $('#sym_page_add').html(r).sym();
                            $('#sym_page_add_home').button();
                            $('#sym_page_add_active').button();
                            $('#sym_page_add_hidden').button();
                            $('#sym_page_add_404').button();
                            $('.sym_page_add .tabs').tabs();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var data={};
                        data['symbiont']='page.db_add';
                        data['name']=$('.sym_page_add .name').val();
                        data['parent']=$('.sym_page_add .parent').val();
                        data['design']=$('.sym_page_add .design').val();
                        data['access']=$('.sym_page_add .access').val();
                        data['title']={};
                        data['keywords']={};
                        data['description']={};
                        $('.sym_page_add .tabs>div').each(function(){
                            lng_id=$(this).find('.lng_id').text();
                            data['title'][lng_id]=$(this).find('.title').val();
                            data['keywords'][lng_id]=$(this).find('.keywords').val();
                            data['description'][lng_id]=$(this).find('.description').val();
                        });
                        
                        if($('.sym_page_add .home').attr('checked')) data['home']=1;
                        if($('.sym_page_add .active').attr('checked')) data['active']=1;
                        if($('.sym_page_add .hidden').attr('checked')) data['hidden']=1;
                        if($('.sym_page_add .p404').attr('checked')) data['404']=1;
                        
                        var d=$(this);
                        $.ajax({
                            data: data,
                            success:function(r){
                                d.dialog("close");
                                location.reload(true);
                            }
                        });
                    }
                }
            });
        }
    });
    $('.sym_light_menu_pages .add').click(function(){
        sym_page_add=$(this).parent().parent().find('.name').first().text();
        $('.sym_page .symbiosis-button-add').click();
    });
    //Update
    $('.sym_light_menu_pages .edit').click(function(){
        t=$(this);
        if($('#sym_page_edit').length){
            $('#sym_page_edit').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_page_edit" title="Page editing"></div>');
            $('#sym_page_edit').dialog({
                modal: true,
                width: 310,
                position: ['center', 100],
                open: function(event, ui) {
                    var name=t.parent().parent().find('.name').first().text();
                    $.ajax({
                        data:{
                            symbiont: 'page.edit',
                            name: name
                        },
                        success:function(r){
                            $('#sym_page_edit').html(r).sym();
                            $('#sym_page_edit_home').button();
                            $('#sym_page_edit_active').button();
                            $('#sym_page_edit_hidden').button();
                            $('#sym_page_edit_404').button();
                            $('#sym_page_edit .tabs').tabs();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var data={};
                        data['symbiont']='page.db_edit';
                        data['id']=$('.sym_page_edit .id').text();
                        data['name']=$('.sym_page_edit .name').val();
                        data['parent']=$('.sym_page_edit .parent').val();
                        data['design']=$('.sym_page_edit .design').val();
                        data['access']=$('.sym_page_edit .access').val();
                        data['title']={};
                        data['keywords']={};
                        data['description']={};
                        $('.sym_page_edit .tabs>div').each(function(){
                            lng_id=$(this).find('.lng_id').text();
                            data['title'][lng_id]=$(this).find('.title').val();
                            data['keywords'][lng_id]=$(this).find('.keywords').val();
                            data['description'][lng_id]=$(this).find('.description').val();
                        });
                        
                        if($('.sym_page_edit .home').attr('checked')) data['home']=1;
                        if($('.sym_page_edit .active').attr('checked')) data['active']=1;
                        if($('.sym_page_edit .hidden').attr('checked')) data['hidden']=1;
                        if($('.sym_page_edit .p404').attr('checked')) data['404']=1;
                        
                        var d=$(this);
                        $.ajax({
                            data: data,
                            success:function(r){
                                d.dialog("close");
                                location.reload(true);
                            }
                        });
                    }
                }
            });
        }
    });
    //Delete
    $('.sym_light_menu_pages .delete').click(function(){
        t=$(this);
        if($('#sym_page_delete').length){
            $('#sym_page_delete').dialog('open');
        }
        else{
            $('#windows').append('<div id="sym_page_delete" title="Page editing"></div>');
            $('#sym_page_delete').dialog({
                modal: true,
                width: 310,
                position: ['center', 100],
                open: function(event, ui) {
                    var name=t.parent().parent().find('.name').text();
                    $.ajax({
                        data:{
                            symbiont: 'page.delete.'+name
                        },
                        success:function(r){
                            $('#sym_page_delete').html(r).sym();
                        },
                        dataType: 'html'
                    });
                },
                buttons:{
                    "Cancel":function(){
                        $(this).dialog("close");
                    },
                    "Ok":function(){
                        var d=$(this);
                        var name=t.parent().parent().find('.name').text();
                        $.ajax({
                            data: {
                                symbiont: 'page.db_delete',
                                name: name
                            },
                            success:function(r){
                                d.dialog("close");
                                if(r.ok!=undefined){
                                    t.parent().parent().slideUp();
                                }
                            }
                        });
                    }
                }
            });
        }
    });
    
});